//
//  LPTEditBankCardViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/6.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTEditBankCardViewController.h"
#import "LPTEditCardTableViewCell.h"
#import "LPTEditBankCardFooterView.h"

@interface LPTEditBankCardViewController ()<UITableViewDelegate,UITableViewDataSource,LPTEditBankCardFooterViewDelegate>

@end

@implementation LPTEditBankCardViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    
}

-(void)initSubViews
{
    self.tableView.tableFooterView = [UIView new];
    
    LPTEditBankCardFooterView *footerView = [[LPTEditBankCardFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    footerView.delegate = self;
    self.tableView.tableFooterView = footerView;
    [self.dataSource addObjectsFromArray:@[@"开户行",@"开户人",@"卡号"]];
    [self.tableView reloadData];
}

-(void)saveBankCard
{
    LPTEditCardTableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSString *bankName = cell1.textField.text;
    
    LPTEditCardTableViewCell *cell2 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    NSString *ballanceHolder = cell2.textField.text;
    
    LPTEditCardTableViewCell *cell3 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    NSString *ballanceNumber = cell3.textField.text;
    
    if (!bankName.length) {
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:@"请输入开户行"];
        return;
        
    }else if (!ballanceHolder.length){
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:@"请输入开户人名字"];
        return;
        
    }else if (!ballanceNumber.length){
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:@"请输入开户人银行卡号"];
        return;
    }
    
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/bank_card_save" withBody:@{@"bank":bankName,@"account_holder":ballanceHolder,@"account_number":ballanceNumber} withMethod:YTKRequestMethodPOST];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh_login_userinfo" object:nil];
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithSuccess:@"保存成功"];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD show];
        [MMProgressHUD dismissWithError:data];
        
    }];
}

#pragma mark - UITableViewDataSource & Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPTEditCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTEditCardTableViewCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.titleLabel.text = @"开户行";
        cell.textField.placeholder = @"请输入银行名称";
        
        if ([[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.bank.length) {
            
            cell.textField.text = [[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.bank;
        }
    }else if (indexPath.row == 1){
        
        cell.titleLabel.text = @"开户人";
        cell.textField.placeholder = @"请输入开户人";
        if ([[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.bank.length) {
            
            cell.textField.text = [[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.account_holder;
        }
        
    }else{
        cell.titleLabel.text = @"卡号  ";
        cell.textField.placeholder = @"请输入卡号";
        cell.textField.keyboardType = UIKeyboardTypeNumberPad;
        if ([[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.bank.length) {
            
            cell.textField.text = [[LTPCacheManager shareInstance] getUserInfoModel].doctor.bank_card.account_number;
        }
    }
    
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - LPTEditBankCardFooterViewDelegate
-(void)editBankCardFooterViewDidClickSaveBtn
{
    [self saveBankCard];
}

@end
